package com.power.structure.linked;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/11 4:48 下午
 */
public class CycleLinkedList {
    public Node head;
    public Node tail;
    private int size;

    class Node {
        Node next;
        int value;
        public Node(int value) {
            this.value = value;
        }
    }
    //默认插入元素的方法
    public void add(int element) {
        Node node = new Node(element);
        if (size == 0) {
            tail = head = node;
        } else if (size == 1) {
            tail = node;
            head.next = tail;
            tail.next = head;
        } else {
//            Node tmp = head;
//            while (tmp != null) {
//                //末尾加上新的值
//                if (tmp.next == tail) {
//                    tmp.next = node;
//                    node.next = tail;
//                    break;
//                }
//                tmp = tmp.next;
//            }
            node.next = tail.next;
            tail.next = node;
            tail = node;
           /* head = node.next;
            node = tail.next;
            tail = node;*/
        }
        size++;
    }

    public void insertFirst() {

    }

    public void display() {
        Node tmp = head;
        int count = 0;
        while (tmp != null && count < size) {
            System.out.print(" " + tmp.value);
            tmp = tmp.next;
            count++;
        }

        System.out.println("\n");
    }

    public static void main(String[] args) {
        CycleLinkedList cycleLinkedList = new CycleLinkedList();
        for (int i = 0; i < 34; i++) {
            cycleLinkedList.add(i);
        }
        cycleLinkedList.display();
    }
}
